package com.wechat.android.app.ui.message;

import android.app.ProgressDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hyphenate.easeui.widget.EaseAlertDialog;
import com.wechat.android.app.R;
import com.wechat.android.app.adapter.SearchFriendAdapter;
import com.wechat.android.app.callback.RecycleViewDivider;
import com.wechat.android.app.db.UserHelper;
import com.wechat.android.app.entity.SearchPhoneFriendModel;
import com.wechat.android.app.global.Constants;
import com.wechat.android.app.ui.BaseActivity;
import com.wechat.android.app.utils.MyLog;
import com.wechat.android.app.utils.PhoneUtils;
import com.wechat.android.app.widget.CustomFontEditText;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/1/16.
 */

public class AddContactActivity extends BaseActivity {
    @BindView(R.id.edit_note)
    CustomFontEditText editText;
    @BindView(R.id.rv_search_friend)
    RecyclerView rvSearchFriend;
    private String toAddUsername;
    private List<SearchPhoneFriendModel> searchPhoneFriendList = new ArrayList<>();
    private SearchFriendAdapter searchFriendAdapter;
    @Override
    protected int attachLayoutRes() {
        return R.layout.em_activity_add_contact;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("\uD81B\uDF0A\uD81B\uDF51\uD81B\uDFC4\uD81B\uDFC0\uD81B\uDF3B\uD81B\uDFD4\uD81B\uDF3D\uD81B\uDFD3\uD81B\uDF16\uD81B\uDF54");
        getSubTitle().setText("\uD81B\uDF16\uD81B\uDFA8\uD81B\uDF50\uD81B\uDF0E\uD81B\uDFAC");
        editText.setHint("\uD81B\uDF2F\uD81B\uDFDF\uD81B\uDFD9\uD81B\uDF50\uD81B\uDF37\uD81B\uDFD1");

        searchFriendAdapter = new SearchFriendAdapter(R.layout.item_search_friend, searchPhoneFriendList);
        rvSearchFriend.setLayoutManager(new LinearLayoutManager(AddContactActivity.this));
        rvSearchFriend.addItemDecoration(new RecycleViewDivider(AddContactActivity.this, LinearLayoutManager.VERTICAL, R.drawable.shape_line_divider_decoration));
        rvSearchFriend.setAdapter(searchFriendAdapter);

        searchFriendAdapter.setAddFriendOnClick(new SearchFriendAdapter.OnAddFriendsListener() {
            @Override
            public void getAddFriendOnClick(SearchPhoneFriendModel data) {
                addRequestFriend(data.getImKey());
            }
        });
    }

    @Override
    protected void updateViews() {

    }


    public void searchContact(View v) {
        final String name = editText.getText().toString();
        toAddUsername = name;
        if (TextUtils.isEmpty(name)) {
            mMStatusDialog.show("\uD81B\uDF37\uD81B\uDFA4\uD81B\uDF51\uD81B\uDF3A\uD81B\uDFD4\uD81B\uDF2F\uD81B\uDFDF\uD81B\uDFD9\uD81B\uDF50\uD81B\uDF37\uD81B\uDFD1\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF16\uD81B\uDFA8\uD81B\uDF50\uD81B\uDF0E\uD81B\uDFAC",null);
            return;
        }
        searchContacts(toAddUsername);

    }



    private void searchContacts(String phone) {
        mMProgressDialog.showNoText();
        HttpParams params = new HttpParams();
        params.put("phone", phone);
        params.put("imKey", UserHelper.getInstance().getUserInfoModel(AddContactActivity.this).getImKey());
        EasyHttp.post(Constants.API_GETUSERBYPHONE)
                .params(params)
                .timeStamp(true)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", "搜索onError==" + e.toString());
                        mMStatusDialog.show("\uD81B\uDF1E\uD81B\uDFA9\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF2F\uD81B\uDF51\uD81B\uDF62\uD81B\uDF3D\uD81B\uDF54\uD81B\uDF16\uD81B\uDF60",null);
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "搜索好友==" + result);
                        String data = PhoneUtils.checkResponseFlag(AddContactActivity.this, result);
                        if (data != null) {
                            Gson gson = new Gson();
                            searchPhoneFriendList = gson.fromJson(data, new TypeToken<List<SearchPhoneFriendModel>>() {
                            }.getType());
                            MyLog.e("yang", "searchPhoneFriendList==" + searchPhoneFriendList.size());
                            searchFriendAdapter.setNewData(searchPhoneFriendList);

                        }
                    }
                });
    }


    private void addRequestFriend(String imKey) {
        mMProgressDialog.showNoText();
        HttpParams params = new HttpParams();
        params.put("imKey", UserHelper.getInstance().getUserInfoModel(AddContactActivity.this).getImKey());
        params.put("friendImkey",imKey);
        EasyHttp.post(Constants.API_ADDFRIEND)
                .params(params)
                .timeStamp(true)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", "onError==" + e.toString());
                        mMStatusDialog.show("\uD81B\uDF1E\uD81B\uDFA9\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF2F\uD81B\uDF51\uD81B\uDF62\uD81B\uDF3D\uD81B\uDF54\uD81B\uDF16\uD81B\uDF60",null);
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "添加好友==" + result);
                       String data = PhoneUtils.checkResponseFlag(AddContactActivity.this,result);
                       if(data!=null){
                           //发送成功,等待验证
                           mMStatusDialog.show("\uD81B\uDF3A\uD81B\uDFBD\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF16\uD81B\uDF60\uD81B\uDF0A\uD81B\uDF58\uD81B\uDF0A\uD81B\uDF59\uD81B\uDF04\uD81B\uDFD4\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF16\uD81B\uDF59",null);
                       }
                    }
                });
    }

    @OnClick({R.id.toolbar_subtitle})
    public void viewOnClick(View view) {
        searchContact(view);
    }

}
